<?php

namespace app\admin\controller\yunfood;

use addons\yunfood\library\Backend;
use app\admin\model\AuthGroup;
use \addons\yunfood\model\Config as ConfigModel;

/**
 * 配置模块
 */
class Config extends Backend
{
    /**
     * @var ConfigModel
     */
    protected $model;

    protected $noNeedRight = ['groupList'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new ConfigModel();
    }

    /**
     * 基础设置
     */
    public function index()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                if (isset($params[ConfigModel::GROUP_BASE]['login_methods'])) {
                    $params[ConfigModel::GROUP_BASE]['login_methods'] =
                        implode(',', $params[ConfigModel::GROUP_BASE]['login_methods']);
                }
                ConfigModel::saveAllAttributes($params);
                $this->success();
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $row = ConfigModel::getAllAttributes();
        $this->view->assign('row', $row);

        $this->view->assign('loginMethods', [
            'phone_captcha' => ['name' => '手机验证码', 'tip' => '手机+验证码方式，手机号必须唯一，必须配置阿里云短信'],
            'wechat_phone'  => ['name' => '微信快捷验证', 'tip' => '手机+验证码方式，手机号必须唯一，可以不用配置阿里云短信']
        ]);
        $this->view->assign('yesOrNo', [1 => '是', 0 => '否']);

        $roleList = AuthGroup::field(['id', 'name'])->select();
        $roleList = array_column($roleList, 'name', 'id');
        $this->view->assign('roleList', $roleList);
        $this->view->assign('defaultH5Domain', ConfigModel::getDefaultH5Domain());

        return $this->view->fetch();
    }

    public function add()
    {
        return;
    }

    public function edit($ids = null)
    {
        return;
    }

    public function del($ids = null)
    {
        return;
    }

    public function multi($ids = null)
    {
        return;
    }
}
